/**
 * 示例-图书 编辑页
 *
 * 作者 chenweihua
 * 日期 2022-02-05 11:07:44
 */
layui.use(['jquery','febs', 'form', 'validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        $view = $('#demoBook-edit'),
        validate = layui.validate;

    form.verify(validate);
    form.render();
    initFormValue();

    function initFormValue() {
        if (demoBookData) {
            form.val("demoBook-edit-form", {
                "id": demoBookData.id,
                "bookName": demoBookData.bookName,
                "shortName": demoBookData.shortName,
                "author": demoBookData.author,
            });
        }
    }

    form.on('submit(demoBook-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        if (data.id && data.id.length > 0) {
            febs.post(ctx + 'demoBook/update', data, function () {
                layer.closeAll();
                febs.alert.success('修改成功');
                febs.reloadPage();
            });
        } else {
            febs.post(ctx + 'demoBook', data, function () {
                layer.closeAll();
                febs.alert.success('新增成功');
                $('#febs-demoBook').find('#query').trigger('click');
            });
        }
    }
});